//================================================================================================================================
// Code to deal with TV channel information
// channels are read in to a structure from an ini file (number, name) pairs
// then when a channel changes a function here is called with the channel number and returns the channel name
//================================================================================================================================
                        
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include "MCE.h"

//================================================================================================================================
// The channels table
//================================================================================================================================
#define NUM_CHANNELS 1024
//
// Structure to hold the channel numbers and names
//
static struct tvchannel_struct {
              int  number;         // Channel Number
              char name[128];      // Channel Name
             } tvchannels [NUM_CHANNELS];
             static struct radiochannel_struct {
              char frequency[80];  // Channel Frequency
              char name[128];      // Channel Name
             } radiochannels[NUM_CHANNELS];
  
static int num_tvchannels_used,num_radiochannels_used;;       // number of valid channels in the table
static char channel_line[256];      // Holds the display line to return to the caller



//================================================================================================================================
// function to read from an ini file into the channels table
//================================================================================================================================
extern int __stdcall MCE_read_ini_file()
{
     FILE *fp;
     int j,k, chan, linelen, channeltype;
     char line[256],str[80];

	 Topen_log();

     Tlog("MCE_read_ini_file","Started");     
     num_tvchannels_used=0;
     num_radiochannels_used=0;
     channeltype=0;        // TV, (radio=1)
     fp=fopen("plugins\\MCE_dll.ini","r");
     if (fp == NULL) { printf("Failed to open ini file"); return 1; }

     while (fgets(line,250,fp))
       {
           linelen=strlen(line);
           if (linelen>0) 
             { if (!strncmp(line,"[TV]",4)) channeltype=0;
               else if (!strncmp(line,"[Radio]",7)) channeltype=1; 
               else if ((line[0]!='/') && (line[0]!=0) && (line[1]!=0)) // Ignore comments and empty lines
                 { if (channeltype==0)
                     { sscanf(line,"%d",&chan);                        // Get channel Number
                       tvchannels[num_tvchannels_used].number=chan;     // Store it then find where the name starts on the line
                       for (j=0; j<linelen; j++) { if ( (line[j]==' ') || (line[j]=='\t') ) {k=j; break;} }                   
                       strcpytonl(tvchannels[num_tvchannels_used++].name,&line[k+1]);  // Store the channel name in the next slot
                       if (num_tvchannels_used<NUM_CHANNELS)
                         { sprintf(str,"TV Channel %d Name %s",chan,tvchannels[num_tvchannels_used-1].name); }
                         else { sprintf(str,"*** Error - Too many TV channels defined"); }
                     }
                   else
                     { for (j=0; j<linelen; j++) { if ( (line[j]==' ') || (line[j]=='\t') ) {k=j; break;} }   
                       strncpy(radiochannels[num_radiochannels_used].frequency,line,k);
                       strcpytonl(radiochannels[num_radiochannels_used++].name,&line[k+1]);  // Store the channel name in the next slot
                       if (num_radiochannels_used<NUM_CHANNELS)
                         { sprintf(str,"Radio Frequency %s Name %s",radiochannels[num_radiochannels_used-1].frequency
                                                               ,radiochannels[num_radiochannels_used-1].name); }
                         else { sprintf(str,"*** Error - Too many Radio channels defined"); }
                     }
                   Tlog("Read Ini",str);
                 }
             }
       }
     Tlog("MCE_read_ini_file","finished");
     fclose(fp);
     return 0;
}


//================================================================================================================================
// function to return a channel name given a channel number
//================================================================================================================================
extern char * __stdcall MCE_dll_get_tvchannel(char *ichan)
{
       int i,cn;
       char line[80];
       
       if (sscanf(ichan,"%d",&cn)!=1) return "Invalid Channel Number";
       else
           {
              for (i=0; i<num_tvchannels_used; i++)
                { if(cn==tvchannels[i].number) return (tvchannels[i].name); }
           }
 //      sprintf(channel_line,"channel %d not found",cn);
       sprintf(channel_line,getst(TAG_ChannelNotFound),cn);
       return channel_line;
}

extern char * __stdcall MCE_dll_get_radiochannel(char *ichan)
{
       int i,cn;
       char line[80];
       
       for (i=0; i<num_radiochannels_used; i++)
         { if(!strncmp(ichan,radiochannels[i].frequency,strlen(ichan))) return (radiochannels[i].name); }
       sprintf(channel_line,getst(TAG_ChannelNotFound),cn);
       return channel_line;
}

